const tools = {
	/**
	 * @description 详细判断是否为手机号
	 * @param {String} str 要匹配的手机号
	 * @returns {Boolean} 返回 匹配结果
	 */
	isPhone(str) {
		let reg = /^1[3456789]\d{9}$/;
		if (reg.test(str)) {
			return true;
		} else {
			return false;
		}
	},
	containsWildcard(str) {
		return ['%', '_', '*', '?', '[', ']'].some(wildcard => str.includes(wildcard));
	},
	isInteger(value) {
		console.log(Number.isInteger(value))
		return Number.isInteger(value);
	},
	isName(str) {
		let reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,5}$/;
		if (reg.test(str)) {
			return true;
		} else {
			return false;
		}
	},
	// 邮箱
	isEmail(email) {
		let emailRegExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
		return emailRegExp.test(email)
	},
	// 页面加载处理
	loadIng(msg) {
		uni.showLoading({
			title: msg || '加载中....',
			mask: true
		})
		setTimeout(function() {
			uni.hideLoading();
		}, 1000)
	},
	showToast(msg) {
		uni.showToast({
			title: msg,
			duration: 2000,
			icon: 'none'
		})
	},
	hideLoading() {
		uni.hideLoading();
	},
	formatDate(timestamp, day) {
		if (('' + timestamp).length <= 10) {
			var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		}else{
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		}
		
		let Y = date.getFullYear() + '-';
		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
		let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
		let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
		let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
		let reluset = ''
		if (day) {
			reluset = Y + M + D
		} else {
			reluset = Y + M + D + h + m + s
		}
		return reluset;
	},
	formatMonth(timestamp){
		if (('' + timestamp).length <= 10) {
			var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		}else{
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		}
		
		let Y = date.getFullYear() + '-';
		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
		let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
		let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
		let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
		let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
		let reluset = ''
		reluset = Y + M
		return reluset;
	},
	formatDay(timestamp) {
		var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let Y = date.getFullYear() + '-';
		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
		let reluset = ''
		reluset = Y + M + D
		return reluset;
	},
	fDate(date) {
		let timestamp = Date.parse(date)
		if (('' + timestamp).length <= 10) {
			timestamp = timestamp * 1000
		}

		var formats = {
			'year': '%n% 年前',
			'month': '%n% 月前',
			'day': '%n% 天前',
			'hour': '%n% 小时前',
			'minute': '%n% 分钟前',
			'second': '%n% 秒前',
		}
		var now = Date.now()
		var seconds = Math.floor((now - timestamp) / 1000)
		var minutes = Math.floor(seconds / 60)
		var hours = Math.floor(minutes / 60)
		var days = Math.floor(hours / 24)
		var months = Math.floor(days / 30)
		var years = Math.floor(months / 12)
		var diffType = ''
		var diffValue = 0
		if (years > 0) {
			diffType = 'year'
			diffValue = years
		} else {
			if (months > 0) {
				diffType = 'month'
				diffValue = months
			} else {
				if (days > 0) {
					diffType = 'day'
					diffValue = days
				} else {
					if (hours > 0) {
						diffType = 'hour'
						diffValue = hours
					} else {
						if (minutes > 0) {
							diffType = 'minute'
							diffValue = minutes
						} else {
							diffType = 'second'
							diffValue = seconds === 0 ? (seconds = 1) : seconds
						}
					}
				}
			}
		}
		return formats[diffType].replace('%n%', diffValue)
	},
	
	// 秒转换成day、hour、minutes、seconds
	formatSecond(seconds) {	
		const day = Math.floor(seconds / (3600 * 24));
		const hour = Math.floor((seconds % (3600 * 24)) / 3600);
		const minute = Math.floor((seconds % 3600) / 60);
		const second = seconds % 60;
		
		if(day>0){
			return [ day + '天', hour + '小时', minute + '分钟',  second + '秒'].filter(Boolean).join(' ')
		}else{
			if(hour>0){
				return [hour + '小时', minute + '分钟',  second + '秒'].filter(Boolean).join(' ')
			}else{
				if(minute>0){
					return [minute + '分钟',  second + '秒'].filter(Boolean).join(' ')
				}else{
					return [second + '秒'].filter(Boolean).join(' ')
				}
			}
		}
	},
}
export default tools;